package com.one.common.model.http.base;

import com.one.common.config.CMemoryData;
import com.one.common.model.bean.UserInfoBean;
import com.one.common.model.http.ApiObserver;
import com.one.common.model.http.RetrofitUtil;
import com.one.common.model.http.callback.ObserverOnNextListener;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.view.base.BaseActivity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;

/**
 * creator: zhulunjun
 * time:    2017/9/5
 * describe: 请求的基类
 * 共有操作放到这里
 *
 * 1. 显示loading，绑定activity生命周期
 * 2. 不显示loading， 绑定生命周期
 * 3. 不显示loading， 不绑定生命周期
 * 4. 带error回调，不带error回调
 */

public abstract class BaseModel<T> {

    public RetrofitUtil retrofitUtil;
    public T mApiService;
    public BaseActivity mActivity;

    public BaseModel(Class<T> c, BaseActivity mActivity) {
        initApi(c);
        this.mActivity = mActivity;
    }

    /**
     * 设置host
     */
    public BaseModel(Class<T> c, BaseActivity mActivity, String host) {
        initApi(c,host);
        this.mActivity = mActivity;
    }
    public BaseModel(Class<T> c) {
        initApi(c);
    }


    /**
     * 初始化api相关操作
     *
     * @param c
     */
    private void initApi(Class<T> c) {
        retrofitUtil = RetrofitUtil.getInstance();
        //添加代理，用于处理session过期
        mApiService = retrofitUtil.getProxy(c);
    }
    /**
     * 设置host
     */
    public void initApi(Class<T> c,String host) {
        retrofitUtil = RetrofitUtil.getInstance();
        //添加代理，用于处理session过期
        mApiService = retrofitUtil.getProxy(c,host);
    }

    /**
     * 显示网络弹窗
     */
    private void showLoading(boolean isShowLoading){
        if(mActivity != null && isShowLoading){
            mActivity.showLoading();
        }
    }
    /**
     * 抽离通用的观察者处理逻辑
     * 回调只有onResult
     *
     * @param <V>        返回类型
     * @param observable 接口返回对象
     * @param listener   回调对象
     */
    public <V> void handleOnResultObserver(Observable<BaseResponse<V>> observable,

                                           ObserverOnResultListener<V> listener, final boolean isShowLoading) {
        if(mActivity!=null) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showLoading(isShowLoading);
                }
            });
        }
        retrofitUtil
                .getObservable(observable, mActivity)
                .subscribe(new ApiObserver<>(listener, mActivity));
    }

    public <V> void handleOnResultObserverT(Observable<V> observable,
                                           ObserverOnResultListener<V> listener, boolean isShowLoading) {
        showLoading(isShowLoading);
        retrofitUtil
                .getObservableT(observable, mActivity)
                .subscribe(new ApiObserver<>(listener, mActivity));
    }

    /**
     * 默认显示loading
     * @param observable
     * @param listener
     * @param <V>
     */
    public <V> void handleOnResultObserver(Observable<BaseResponse<V>> observable,
                                                            ObserverOnResultListener<V> listener){
        handleOnResultObserver(observable, listener, true);
    }

    /**
     * 抽离通用的观察者处理逻辑
     * 回调有onnext 和 onResult
     *
     * @param observable 接口返回对象
     * @param listener   回调对象
     * @param <V>        返回类型
     */
    public <V> void handleOnNextObserver(Observable<BaseResponse<V>> observable,
                                                          ObserverOnNextListener<V> listener, boolean isShowLoading) {
        showLoading(isShowLoading);
        retrofitUtil
                .getObservable(observable, mActivity)
                .subscribe((new ApiObserver<>(listener, mActivity)));
    }

    /**
     * 默认显示loading
     * @param observable
     * @param listener
     * @param <V>
     */
    public <V> void handleOnNextObserver(Observable<BaseResponse<V>> observable,
                                                          ObserverOnNextListener<V> listener){
        handleOnNextObserver(observable, listener, true);
    }


    /**
     * 抽离通用的观察者处理逻辑
     * 回调有onnext 和 onResult
     * 不绑定生命周期，不显示loading
     *
     * @param observable 接口返回对象
     * @param listener   回调对象
     * @param <V>        返回类型
     */
    public <V> void handleOnNextObserverNoActivity(Observable<BaseResponse<V>> observable,
                                                                    ObserverOnNextListener<V> listener) {
        retrofitUtil
                .getObservable(observable)
                .subscribe((new ApiObserver<>(listener)));
    }

    /**
     * 抽离通用的观察者处理逻辑
     * 回调只有onResult
     * 不绑定生命周期，不显示loading
     *
     * @param <V>        返回类型
     * @param observable 接口返回对象
     * @param listener   回调对象
     */
    public <V> void handleOnResultObserverNoActivity(Observable<BaseResponse<V>> observable,
                                                                      ObserverOnResultListener<V> listener) {
        retrofitUtil
                .getObservable(observable)
                .subscribe(new ApiObserver<>(listener));
    }


    /**
     * 获取默认参数
     * 用户id 和 用户token
     */
    public Map<String, String> getParams(){
        Map<String, String> map = new HashMap<>();
        UserInfoBean bean = CMemoryData.getUserInfo();
        map.put("member_id", bean.getMember_id()+"");
        map.put("member_token", bean.getMember_token());
        return map;
    }

}
